<template>
  <div class="container">
    <!-- 精选题库 -->
    <div class="bgc">
      <div class="top">
        <div class="illustrate">说明：目前支持学科和关键字条件筛选</div>
        <div class="top_right">
          <el-button
            type="success"
            @click="$router.push('/test-question-input')"
          >
            <i class="el-icon-edit" />
            新增试题
          </el-button>
        </div>
      </div>
      <!-- form表单  搜索 -->
      <el-form :model="formInline" label-width="70px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="学科">
              <el-select
                v-model="formInline.subjectID"
                placeholder="请选择"
                @change="SubjectsFn"
              >
                <el-option
                  v-for="item in SubjectsList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select v-model="formInline.catalogID" placeholder="请选择">
                <div v-if="formInline.subjectID">
                  <el-option
                    v-for="item in DirectoryList"
                    :key="item.id"
                    :label="item.directoryName"
                    :value="item.id"
                  />
                </div>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select v-model="formInline.tags" placeholder="请选择">
                <div v-if="formInline.subjectID">
                  <el-option
                    v-for="item in TagsList"
                    :key="item.id"
                    :label="item.tagName"
                    :value="item.id"
                  />
                </div>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input
                v-model="formInline.keyword"
                placeholder="根据题干搜索"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select v-model="formInline.questionType" placeholder="请选择">
                <el-option
                  v-for="item in typesOf"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select v-model="formInline.difficulty" placeholder="请选择">
                <el-option
                  v-for="item in difficulty"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select v-model="formInline.direction" placeholder="请选择">
                <el-option
                  v-for="item in directionlist"
                  :key="item.name"
                  :label="item.name"
                  :value="item.name"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select v-model="formInline.creatorID" placeholder="请选择">
                <el-option
                  v-for="item in enterlist"
                  :key="item.id"
                  :label="item.username"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="题干备注">
              <el-input v-model="formInline.remarks" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="formInline.shortName" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-row :gutter="10">
                <el-col :span="22">
                  <el-col :span="12">
                    <el-select
                      v-model="formInline.province"
                      placeholder="请选择"
                    >
                      <el-option label="区域一" value="shanghai" />
                      <el-option label="区域二" value="beijing" />
                    </el-select>
                  </el-col>
                  <el-col :span="12">
                    <el-select v-model="formInline.city" placeholder="请选择">
                      <el-option label="区域一" value="shanghai" />
                      <el-option label="区域二" value="beijing" />
                    </el-select>
                  </el-col>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div style="float: right">
              <el-button @click="closeFn">清除</el-button>
              <el-button type="primary" @click="searchFn">搜索</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-tabs v-model="activeName" type="card">
      <el-tab-pane label="全部" name="first">
        <div class="bgc" style="margin: 10px 0; background-color: #e2dfdf">
          数据一共 {{ total }} 条
        </div>
        <div class="bgc">
          <common-label :table-data="tableData" @editState="editState" />
        </div>
      </el-tab-pane>
      <el-tab-pane label="待审核" name="second">
        <div class="bgc" style="margin: 10px 0; background-color: #e2dfdf">
          数据一共 {{ totalAudit }} 条
        </div>
        <div class="bgc">
          <common-label :table-data="AuditListL" @editState="editState" />
        </div>
      </el-tab-pane>
      <el-tab-pane label="已审核" name="third">
        <div class="bgc" style="margin: 10px 0; background-color: #e2dfdf">
          数据一共 {{ totalAudited }} 条
        </div>
        <div class="bgc">
          <common-label :table-data="AuditedListL" @editState="editState" />
        </div>
      </el-tab-pane>
      <el-tab-pane label="已拒绝" name="fourth">
        <div class="bgc" style="margin: 10px 0; background-color: #e2dfdf">
          数据一共 {{ totalRefuse }} 条
        </div>
        <div class="bgc">
          <common-label :table-data="refuseListL" @editState="editState" />
        </div>
      </el-tab-pane>
    </el-tabs>
    <div class="block">
      <el-pagination
        background
        :page-sizes="[10, 5, 2, 1]"
        :page-size="formInline.pagesize"
        layout="->, prev,pager,next,sizes,jumper"
        :current-page="formInline.page"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import commonLabel from './common-label.vue'
import { getChoiceListApi } from '@/api/questionBank'
import {
  getSubjectsListApi,
  getDirectoryListApi,
  getTagsListApi
} from '@/api/subject'
import { getUserListSimpleApi } from '@/api/backstage'
export default {
  components: { commonLabel },
  data() {
    return {
      // 搜索表单
      formInline: {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸
        subjectID: null, // 学科
        catalogID: null, // 目录
        tags: '', // 标签名称
        keyword: '', // 	关键字
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: null, // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '', // 企业所在地省份
        city: '', // 企业所在城市
        chkState: '' // 审核状态
      },
      total: 0, // 数据总条数
      tableData: [], // 全部
      totalAudit: 0, // 待审核总条数
      AuditListL: [], // 待审核
      totalAudited: 0, // 已审核总条数
      AuditedListL: [], // 已审核
      totalRefuse: 0, // 已拒绝总条数
      refuseListL: [], // 已拒绝
      SubjectsList: [], // 学科列表
      SubjectsId: '', // 学科id
      DirectoryList: [], // 目录列表
      TagsList: [], // 标签列表
      // 试题类型
      typesOf: [
        { value: 1, label: '单选' },
        { value: 2, label: '多选' },
        { value: 3, label: '简单' }
      ],
      // 难度
      difficulty: [
        { value: 1, label: '简单' },
        { value: 2, label: '一般' },
        { value: 3, label: '困难' }
      ],
      // 方向
      directionlist: [
        { name: 'o2o' },
        { name: '外包服务' },
        { name: '企业服务' },
        { name: '互联网金融' },
        { name: '企业咨询' },
        { name: '互联网' },
        { name: '电子商务' },
        { name: '其他' }
      ],
      // 录入人
      enterlist: [],
      activeName: 'first'
    }
  },
  async created() {
    const { data } = await getSubjectsListApi()
    this.SubjectsList = data
    const user = await getUserListSimpleApi()
    this.enterlist = user.data
    console.log()
    this.getChoiceList()
  },
  methods: {
    // 精选题库列表方法
    async getChoiceList() {
      this.formInline.chkState = null
      const { data } = await getChoiceListApi(this.formInline)
      this.tableData = data.items
      this.total = data.counts
      // 待审核
      this.formInline.chkState = '0'
      const Audit = await getChoiceListApi(this.formInline)
      this.AuditListL = Audit.data.items
      this.totalAudit = Audit.data.counts
      // 已审核
      this.formInline.chkState = '1'
      const Audited = await getChoiceListApi(this.formInline)
      this.AuditedListL = Audited.data.items
      this.totalAudited = Audited.data.counts
      // 已拒绝
      this.formInline.chkState = '2'
      const refuse = await getChoiceListApi(this.formInline)
      this.refuseListL = refuse.data.items
      this.totalRefuse = refuse.data.counts
      // 全部
      // this.tableData = [
      //   ...this.AuditListL,
      //   ...this.AuditedListL,
      //   ...this.refuseListL
      // ]
      // this.total = this.totalAudit + this.totalAudited + this.totalRefuse
    },
    // 每页条数发生变化
    handleSizeChange(val) {
      console.log(val)
      this.formInline.pagesize = val
      this.getChoiceList()
    },
    // 页码
    handleCurrentChange(val) {
      console.log(val)
      this.formInline.page = val
      this.getChoiceList()
    },
    // 清空搜索框
    closeFn() {
      this.formInline = {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸
        subjectID: '', // 学科
        catalogID: '', // 目录
        tags: '', // 标签名称
        keyword: '', // 	关键字
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: '', // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '', // 企业所在地省份
        city: '', // 企业所在城市
        chkState: '' // 审核状态
      }
      this.getChoiceList()
    },
    // 搜索
    searchFn() {
      this.getChoiceList()
    },
    // 学科选中
    async SubjectsFn(id) {
      console.log(id)
      this.SubjectsId = id
      const { data } = await getDirectoryListApi(id)
      this.DirectoryList = data.items
      const tags = await getTagsListApi(id)
      this.TagsList = tags.data.items
      console.log(tags.data.items)
    },
    editState() {
      this.getChoiceList()
    }
  }
}
</script>
<style scoped lang="scss">
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.bgc {
  background-color: #fff;
  padding: 10px;
  border-radius: 10px;
}
.top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  .illustrate {
    color: rgb(221, 104, 104);
    font-size: 12px;
  }
}
.el-select {
  width: 100%;
}
</style>
